package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.NoticeType;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqNoticeComment;
import com.ruoyi.wq.domain.vo.WqNoticeCommentVO;
import com.ruoyi.wq.mapper.WqNoticeCommentMapper;
import com.ruoyi.wq.service.IWqNoticeCommentService;

/**
 * 评论通知Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-01
 */
@Service
public class WqNoticeCommentServiceImpl extends ServiceImpl<WqNoticeCommentMapper, WqNoticeComment> implements IWqNoticeCommentService {
    
	@Autowired
    private WqNoticeCommentMapper wqNoticeCommentMapper;
	
	@Autowired
	private ISysUserService sysUserService;

    /**
     * 查询评论通知
     * 
     * @param id 评论通知主键
     * @return 评论通知
     */
    @Override
    public WqNoticeComment selectWqNoticeCommentById(String id)
    {
        return wqNoticeCommentMapper.selectWqNoticeCommentById(id);
    }

    /**
     * 查询评论通知列表
     * 
     * @param wqNoticeComment 评论通知
     * @return 评论通知
     */
    @Override
    public List<WqNoticeComment> selectWqNoticeCommentList(WqNoticeComment wqNoticeComment)
    {
        return wqNoticeCommentMapper.selectWqNoticeCommentList(wqNoticeComment);
    }

    /**
     * 新增评论通知
     * 
     * @param wqNoticeComment 评论通知
     * @return 结果
     */
    @Override
    public int insertWqNoticeComment(WqNoticeComment wqNoticeComment)
    {
        wqNoticeComment.setCreateTime(DateUtils.getNowDate());
        return wqNoticeCommentMapper.insertWqNoticeComment(wqNoticeComment);
    }

    /**
     * 修改评论通知
     * 
     * @param wqNoticeComment 评论通知
     * @return 结果
     */
    @Override
    public int updateWqNoticeComment(WqNoticeComment wqNoticeComment)
    {
        wqNoticeComment.setUpdateTime(DateUtils.getNowDate());
        return wqNoticeCommentMapper.updateWqNoticeComment(wqNoticeComment);
    }

    /**
     * 批量删除评论通知
     * 
     * @param ids 需要删除的评论通知主键
     * @return 结果
     */
    @Override
    public int deleteWqNoticeCommentByIds(String[] ids)
    {
        return wqNoticeCommentMapper.deleteWqNoticeCommentByIds(ids);
    }

    /**
     * 删除评论通知信息
     * 
     * @param id 评论通知主键
     * @return 结果
     */
    @Override
    public int deleteWqNoticeCommentById(String id)
    {
        return wqNoticeCommentMapper.deleteWqNoticeCommentById(id);
    }

	@Override
	public TableDataInfo getPageList(WqNoticeComment wqNoticeComment) {
		QueryWrapper<WqNoticeComment> qw = Condition.getQueryWrapper(wqNoticeComment);
        qw.setEntity(wqNoticeComment);
        qw.orderByDesc("create_time");
        qw.eq("dynamic_user_id", SecurityUtils.getUserId());
        qw.or();
        qw.eq("comment_user_id", SecurityUtils.getUserId());
        qw.or();
        qw.eq("reply_user_id", SecurityUtils.getUserId());
        IPage<WqNoticeComment> page = baseMapper.selectPage(Condition.getPage(wqNoticeComment), qw);
        List<WqNoticeCommentVO> noticeVOList = new ArrayList<WqNoticeCommentVO>();
        for(WqNoticeComment notice : page.getRecords()){
        	WqNoticeCommentVO noticeVo = new WqNoticeCommentVO();
        	BeanUtils.copyProperties(notice, noticeVo);
        	if(notice.getCommentUserId() == null){
        		noticeVo.setNoticeType(NoticeType.COMMENT.getCode());
        	}else if(notice.getCommentUserId() != null && notice.getReplyUserId() == null){
        		noticeVo.setNoticeType(NoticeType.COMMENT_REPLY.getCode());
        	}else if(notice.getReplyUserId() != null){
        		noticeVo.setNoticeType(NoticeType.REPLY.getCode());
        	}
        	noticeVo.setTime(DateUtils.getTime(notice.getCreateTime()));
        	noticeVOList.add(noticeVo);
        }
        return Condition.getTableDataInfo(noticeVOList, page.getTotal());
	}

	@Override
	public Integer getUnReadNum(Long userId) {
		WqNoticeComment notice = new WqNoticeComment();
		QueryWrapper<WqNoticeComment> qw = Condition.getQueryWrapper(notice);
		notice.setIsRead("0");
		notice.setNoticeUserId(userId);
		qw.setEntity(notice);
		return baseMapper.selectCount(qw);
	}

	@Override
	public void updateGiveRead() {
		UpdateWrapper<WqNoticeComment> updateWrapper = new UpdateWrapper<>();
		WqNoticeComment entity = new WqNoticeComment();
		entity.setNoticeUserId(SecurityUtils.getUserId());
		entity.setIsRead("0");
		updateWrapper.setEntity(entity);
		
		WqNoticeComment noticeComment = new WqNoticeComment();
		noticeComment.setIsRead("1");
		baseMapper.update(noticeComment, updateWrapper);
		
		SysUser user = new SysUser();
		user.setUserId(SecurityUtils.getUserId());
		user.setCommentNewNum(0);
		sysUserService.updateById(user);
	}
}
